From 5771e976bd973f80d62329df15eb4102d97587c3 Mon Sep 17 00:00:00 2001
From: Pierre Joye <pierre.php@gmail.com>
Date: Wed, 25 Aug 2021 12:52:45 +0700
Subject: [PATCH 1/2] Fix #415, Assuming TopOfs and LeftOfs zero, we can safely
 skip any (x,y) out of the previous image bounds

---
 src/gd_gif_out.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/gd_gif_out.c b/src/gd_gif_out.c
index f1d73fd9..c1e54fa2 100644
--- a/src/gd_gif_out.c
+++ b/src/gd_gif_out.c
@@ -745,6 +745,8 @@ static int _gdImageGifAnimAddCtx(gdImagePtr im, gdIOCtxPtr out,
 			/* Then the bottom row */
 			for (y = tim->sy - 1; y > min_y; --y) {
 				for (x = 0; x < tim->sx; ++x) {
+					if (!gdImageBoundsSafe(prev_tim, x,y))
+						continue;
 					if (!comparewithmap
 					        (prev_tim, tim,
 					         prev_tim->pixels[y][x],

From 4cd5542152afa8409e688297164a08bf03440197 Mon Sep 17 00:00:00 2001
From: Pierre Joye <pierre.php@gmail.com>
Date: Wed, 25 Aug 2021 17:48:03 +0700
Subject: [PATCH 2/2] fix #415, the same is needed in the other section, add
 test

---
 src/gd_gif_out.c         |  4 ++++
 tests/gif/CMakeLists.txt |  1 +
 tests/gif/bug00415.c     | 27 +++++++++++++++++++++++++++
 3 files changed, 32 insertions(+)
 create mode 100644 tests/gif/bug00415.c

diff --git a/src/gd_gif_out.c b/src/gd_gif_out.c
index c1e54fa2..526a221a 100644
--- a/src/gd_gif_out.c
+++ b/src/gd_gif_out.c
@@ -768,6 +768,8 @@ static int _gdImageGifAnimAddCtx(gdImagePtr im, gdIOCtxPtr out,
 			/* left side */
 			for (x = 0; x < min_x; ++x) {
 				for (y = min_y; y <= max_y; ++y) {
+					if (!gdImageBoundsSafe(prev_tim, x,y))
+						continue;
 					if (!comparewithmap
 					        (prev_tim, tim,
 					         prev_tim->pixels[y][x],
@@ -783,6 +785,8 @@ static int _gdImageGifAnimAddCtx(gdImagePtr im, gdIOCtxPtr out,
 			/* right side */
 			for (x = tim->sx - 1; x > max_x; --x) {
 				for (y = min_y; y <= max_y; ++y) {
+					if (!gdImageBoundsSafe(prev_tim, x,y))
+						continue;
 					if (!comparewithmap
 					        (prev_tim, tim,
 					         prev_tim->pixels[y][x],
diff --git a/tests/gif/CMakeLists.txt b/tests/gif/CMakeLists.txt
index d071874f..8349ace5 100644
--- a/tests/gif/CMakeLists.txt
+++ b/tests/gif/CMakeLists.txt
@@ -11,6 +11,7 @@ LIST(APPEND TESTS_FILES
 	bug00006
 	bug00060
 	gif_im2im
+	bug00415
 )
 
 IF(PNG_FOUND)
diff --git a/tests/gif/bug00415.c b/tests/gif/bug00415.c
new file mode 100644
index 00000000..e95df28d
--- /dev/null
+++ b/tests/gif/bug00415.c
@@ -0,0 +1,27 @@
+#include <gd.h>
+#include "gdtest.h"
+
+int main()
+{
+    gdImagePtr im1, im2;
+
+    im1 = gdImageCreate(100, 100);
+    gdImageColorAllocate(im1, 0, 0, 0);
+    im2 = gdImageCreate(10, 10);
+    gdImageColorAllocate(im2, 255, 255, 255);
+
+    FILE *fp = gdTestTempFp();
+    if (!fp) return 4;
+    gdImageGifAnimBegin(im1, fp, 0, 0);
+    gdImageGifAnimAdd(im1, fp, 1, 0, 0, 100, 1, NULL);
+    gdImageGifAnimAdd(im2, fp, 1, 0, 0, 100, 1, im1);
+    // replacing `im2` with `NULL` in the following line succeeds
+    gdImageGifAnimAdd(im1, fp, 1, 0, 0, 100, 1, im2);
+    gdImageGifAnimEnd(fp);
+    fclose(fp);
+
+    gdImageDestroy(im1);
+    gdImageDestroy(im2);
+
+    return 0;
+}
\ No newline at end of file
